package com.shuaitao.newsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuaitao.newsystem.bean.News;
import com.shuaitao.newsystem.service.CommentsService;
import com.shuaitao.newsystem.bean.Comments;
import com.shuaitao.newsystem.mapper.CommentsMapper;
import com.shuaitao.newsystem.utils.R;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 24820
* @description 针对表【comments】的数据库操作Service实现
* @createDate 2023-06-21 17:04:39
*/
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
    implements CommentsService {

    @Override
    public IPage getDetailComments(Integer nid, Integer pageNum) {
        Page<Comments> commentsPage = new Page<>(pageNum, 10);
        IPage<Comments> page = this.page(commentsPage, new QueryWrapper<Comments>().eq("cnid", nid).orderByDesc("cdate"));
        if (page.getRecords().size() == 0 && pageNum > 1) {
            commentsPage.setCurrent(pageNum - 1);
            page = this.page(commentsPage, new QueryWrapper<Comments>().eq("cnid", nid).orderByDesc("cdate"));
        }

        return page;
    }

    @Override
    public R delComment(Integer cid) {
        boolean b = this.remove(new QueryWrapper<Comments>().eq("cid", cid));
        if (b) {
            return  R.ok();
        }
        return R.error(101,"删除失败");
    }
}




